home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / dfpp01.zip / EDITBOX.CPP < prev    next >
C/C++ Source or Header  |  1992-11-21  |  4KB  |  227 lines

  1. // ------------- editbox.cpp
  2.  
  3. #include <ctype.h>
  4. #include "desktop.h"
  5. #include "editbox.h"
  6.  
  7. // ----------- common constructor code
  8. void EditBox::OpenWindow()
  9. {
  10.     windowtype = EditboxWindow;
  11.     if (windowstate == CLOSED)
  12.         TextBox::OpenWindow();
  13.     column = 0;
  14.     changed = False;
  15.     text = new String(bufflen);
  16.     BuildTextPointers();
  17. }
  18.  
  19. void EditBox::CloseWindow()
  20. {
  21.     // destructor code
  22.     // .....
  23.     TextBox::CloseWindow();
  24. }
  25.  
  26. Bool EditBox::SetFocus()
  27. {
  28.     Bool rtn = TextBox::SetFocus();
  29.     if (rtn)    {
  30.         ResetCursor();
  31.         desktop.cursor().Show();
  32.     }
  33.     return rtn;
  34. }
  35.  
  36. void EditBox::ResetFocus()
  37. {
  38.     desktop.cursor().Hide();
  39.     TextBox::ResetFocus();
  40. }
  41.  
  42. // -------- process keystrokes
  43. void EditBox::Keyboard(int key)
  44. {
  45.     int shift = desktop.keyboard().GetShift();
  46.     if ((shift & ALTKEY) == 0)    {
  47.         switch (key)    {
  48.             case HOME:
  49.                 Home();
  50.                 return;
  51.             case END:
  52.                 End();
  53.                 return;
  54.             case CTRL_FWD:
  55.                 NextWord();
  56.                 return;
  57.             case CTRL_BS:
  58.                 PrevWord();
  59.                 return;
  60.             case FWD:
  61.                 Forward();
  62.                 return;
  63.             case BS:
  64.                 Backward();
  65.                 return;
  66.             case RUBOUT:
  67.                 if (column == 0)
  68.                     break;
  69.                 Backward();
  70.                 // --- fall through
  71.             case DEL:
  72.                 DeleteCharacter();
  73.                 return;
  74.             default:
  75.                 if (!isprint(key))
  76.                     break;
  77.                 // --- all printable keys get processed by editbox
  78.                 CharacterEntry(key);
  79.                 return;
  80.         }
  81.     }
  82.     TextBox::Keyboard(key);
  83. }
  84.  
  85. // -------- paint the editbox
  86. void EditBox::Paint()
  87. {
  88.     TextBox::Paint();
  89.     ResetCursor();
  90. }
  91.  
  92. // -------- move the editbox
  93. void EditBox::Move(int x, int y)
  94. {
  95.     TextBox::Move(x, y);
  96.     ResetCursor();
  97. }
  98.  
  99. // --------- clear the text from the editbox
  100. void EditBox::ClearText()
  101. {
  102.     TextBox::ClearText();
  103.     column = 0;
  104.     changed = False;
  105.     ResetCursor();
  106. }
  107.  
  108. void EditBox::Home()
  109. {
  110.     column = wleft = 0;
  111.     Paint();
  112.     ResetCursor();
  113. }
  114.  
  115. void EditBox::End()
  116. {
  117.     column = textwidth;
  118.     if (column - wleft >= ClientWidth())    {
  119.         wleft = column - ClientWidth() + 1;
  120.         Paint();
  121.     }
  122.     ResetCursor();
  123. }
  124.  
  125. void EditBox::NextWord()
  126. {
  127.     while ((*text)[column] != ' ' && (*text)[column])
  128.         Forward();
  129.     while ((*text)[column] == ' ')
  130.         Forward();
  131. }
  132.  
  133. void EditBox::PrevWord()
  134. {
  135.     Backward();
  136.     while ((*text)[column] == ' ' && column != 0)
  137.         Backward();
  138.     while ((*text)[column] != ' ' && column != 0)
  139.         Backward();
  140.     if ((*text)[column] == ' ')
  141.         Forward();
  142. }
  143.  
  144. void EditBox::Forward()
  145. {
  146.     if ((*text)[column])    {
  147.         column++;
  148.         if (column-wleft == ClientWidth())
  149.             ScrollLeft();
  150.         ResetCursor();
  151.     }
  152. }
  153.  
  154. void EditBox::Backward()
  155. {
  156.     if (column)    {
  157.         if (column == wleft)
  158.             ScrollRight();
  159.         --column;
  160.         ResetCursor();
  161.     }
  162. }
  163.  
  164. void EditBox::DeleteCharacter()
  165. {
  166.     if ((*text)[column])    {
  167.         String *ns = new String;
  168.         if (column)
  169.             *ns = text->left(column);
  170.         int rt = text->Strlen()-column-1;
  171.         if (rt > 0)
  172.             *ns += text->right(rt);
  173.         delete text;
  174.         text = ns;
  175.         BuildTextPointers();
  176.         Paint();
  177.         changed = True;
  178.     }
  179. }
  180.  
  181. void EditBox::CharacterEntry(int key)
  182. {
  183.  
  184.     // --- test typing at end of buffer
  185.     if (column == bufflen)    {
  186.         desktop.speaker().Beep();
  187.         return;
  188.     }
  189.  
  190.     if (desktop.keyboard().InsertMode() && (*text)[column])    {
  191.         // ---- shift the text to make room for new character
  192.         String *ns = new String;
  193.         if (column)
  194.             *ns = text->left(column);
  195.         *ns += " ";
  196.         int rt = text->Strlen()-column;
  197.         if (rt > 0)
  198.             *ns += text->right(rt);
  199.         delete text;
  200.         text = ns;
  201.     }
  202.     (*text)[column] = (char) key;
  203.     BuildTextPointers();
  204.     Forward();
  205.     Paint();
  206.     changed = True;
  207. }
  208.  
  209. void EditBox::SetCursor(int x, int y)
  210. {
  211.     desktop.cursor().SetPosition(
  212.         x+ClientLeft()-wleft, y+ClientTop()-wtop);
  213. }
  214.  
  215. void EditBox::LeftButton(int mx, int my)
  216. {
  217.     if (ClientRect().Inside(mx, my))    {
  218.         column = min(text->Strlen(), mx-ClientLeft()+wleft);
  219.         ResetCursor();
  220.     }
  221.     else
  222.         TextBox::LeftButton(mx, my);
  223. }
  224.  
  225.  
  226.  
  227.